<?php

 /***
 * Class CW_RegistryClass is a repository class that holds other referenced objects.
 ***/
class CW_RegistryClass {

	/***
	 * A list of registered objects
	* @access Private
	 * @type Array
	 ***/
	private $registered_objects;


   // ! Constructor Method

   /**
	* Instantiates class and defines instance variables.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Void
	*/
	public function __construct()
	{
		$this->registered_objects = array();
	}


   // ! Executor Method

   /**
	* Adds a new object to the registry.
	*
	* @param $key Name of the new object.
	* @param $object The object passed by reference.
	* @param $overwrite Check if object exists, overwrite if true.
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Boolean
	*/
	public function registerObject($name, &$Object, $overwrite = true)
	{
		if($this->checkIfObjectExists($name) && $overwrite)
		{
			$this->killObject($name);
		}
		else if($this->checkIfObjectExists($name))
		{
			return false;
		}

		$this->registered_objects[] = $name;

		$this->$name = &$Object;

		return true;
	}


   // ! Executor Method

   /**
	* Destroys a registered object.
	*
	* @param $objects A mixed value of objects to kill.
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Boolean
	*/
	public function killObject($objects = '*') // Value of '*' kills ALL objects.
	{
		if($objects == '*')
		{
			foreach($this->registered_objects as $object)
			{
				unset($this->$object);
			}

			$this->registered_objects = array();

			return true;
		}

		if(is_array($objects))
		{
			foreach($objects as $object)
			{
				if($this->checkIfObjectExists($object))
				{
					unset($this->$object);
				}
			}

			for($i = 0; $i < sizeof($this->registered_objects); $i++)
			{
				if(in_array($this->registered_objects[$i], $objects))
				{
					unset($this->registered_objects[$i]);
				}
			}

			return true;
		}

		return false;
	}


   // ! Accessor Method

   /**
	* Used to check whether a method exists within
	* a specified registered object.
	*
	* @param $object Object in which the method resides.
	* @param $method Title of method to check.
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Boolean
	*/
	public function checkIfMethodExists($object, $method)
	{
		if(false == $this->checkIfObjectExists($object))
		{
			return false;
		}

		return method_exists($this->$object, $method);
	}


   // ! Executor Method

   /**
	* A special interface method that uses error checking to ensure
	* safe execution of specified methods. Methods to be called must
	* be specifically designed to be used within this method.
	*
	* @param String $object Object in which the method resides.
	* @param String $method Title of method to execute.
	* @param Array $params Parameters to be used by specified method.
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Mixed
	*/
	public function execMethod($object, $method, $params = array())
	{
		if($this->checkIfMethodExists($object, $method))
		{
			return $this->$object->$method($params);
		}

		return false;
	}


   // ! Executor Method

   /**
	* Destroys a session's value.
	*
	* @param String $name Name of the object to find.
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Boolean
	*/
	public function checkIfObjectExists($name)
	{
		return isset($this->$name);
	}


   // ! Accessor Method

   /**
	* Fetches a list of currently registered objects.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Array
	*/
	public function getRegisteredObjectList()
	{
		return $this->registered_objects;
	}
}

?>